// src/api/api.js
import {request} from '@/plugins/axios' // 确保这里的路径正确

// 图书管理接口
export function getbookinfopage(data,pagenum) { // <--- 关键修改：将 getbookinfopage 改名为 getBookList
    return request({
        url: '/bookinfo/page?pagenum='+pagenum,
        method: 'POST',
        data: data
    })
}

export function getbookType() { // 获取图书分类树形结构
    return request({
        url: '/bookcategory/tree',
        method: 'GET',
    })
}

export function validateBookname(bookname,bookid) { // 验证图书名称唯一性
    return request({
        url: '/bookinfo/validateBookname?bookname='+bookname+'&bookid='+bookid,
        method: 'GET',
    })
}

export function saveOrUpdateBook(data) { // 新增或修改图书
    return request({
        url: '/bookinfo/add',
        method: 'post',
        data: data
    })
}

export function delbook(bookid) { // 删除图书
    return request({
        url: '/bookinfo/'+bookid,
        method: 'delete',
    })
}

// 图书分类管理接口
export function saveOrUpdateBookType(data) { // 新增或修改图书分类
    return request({
        url: '/bookcategory/add',
        method: 'post',
        data: data
    })
}

export function delBookType(booktypeid) { // 删除图书分类
    return request({
        url: '/bookcategory/' + booktypeid,
        method: 'delete',
    })
}

export function validateBookTypeName(booktypename, booktypeid) { // 验证图书分类名称唯一性
    return request({
        url: '/bookcategory/validateBookTypeName?booktypename=' + booktypename + '&booktypeid=' + booktypeid,
        method: 'GET',
    })
}

// 用户认证接口
export function login(username, password) {
    return request({
        url: '/user/login',
        method: 'post',
        data: {
            username: username,
            userpassword: password,
        }
    })
}
export function register(username, password) {
    return request({
        url: '/user/register',
        method: 'post',
        data: {
            username: username,
            userpassword: password,
        }
    })
}

// 借阅管理接口
export function borrowBook(bookid, username) {
    return request({
        url: '/borrow/add',
        method: 'post',
        data: {
            bookid: bookid,
            username: username // <-- 将 username 放到请求体中
        }
    });
}

export function returnBook(borrowid) {
    return request({
        url: `/borrow/return/${borrowid}`,
        method: 'put'
    });
}

// 关键修改：通过 username 查询当前借阅记录的函数
export function getMyCurrentBorrowsByUsername(pagenum, username) {
    if (!username) {
        console.error("Error: Username not provided for getMyCurrentBorrowsByUsername.");
        return Promise.reject(new Error("用户名缺失，无法获取当前借阅记录。"));
    }
    return request({
        url: '/borrow/my-current-borrows',
        method: 'get',
        params: { pagenum: pagenum, username: username }, // <-- 传递 username 作为查询参数
    });
}

// 关键修改：通过 username 查询历史借阅记录的函数
export function getMyBorrowHistoryByUsername(pagenum, username) {
    if (!username) {
        console.error("Error: Username not provided for getMyBorrowHistoryByUsername.");
        return Promise.reject(new Error("用户名缺失，无法获取历史借阅记录。"));
    }
    return request({
        url: '/borrow/my-history',
        method: 'get',
        params: { pagenum: pagenum, username: username }, // <-- 传递 username 作为查询参数
    });
}